package com.turing.post.selfwebservice.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.selfwebservice.util.CardHolderUser;
import com.turing.post.selfwebservice.util.CardHolderUserDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 自助服务网站注册用户数据访问实现类
 * 
 * @author 李治
 * 
 */
@Stateless(mappedName = "selfwebservice.impl.CardHolderUserDaoImpl")
@Remote
@Local
public class CardHolderUserDaoImpl implements CardHolderUserDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteCardHolderUser(BigInteger cardHolderUserId)
			throws PCPSApplicationException {
		CardHolderUser cardHolderUser = getCardHolderUserByCardHolderUserId(cardHolderUserId);
		if (null != cardHolderUser)
			GeneralDao.remove(_em, cardHolderUser);

	}

	@Override
	public CardHolderUser getCardHolderUserByCardHolderUserId(
			BigInteger cardHolderUserId) {
		return _em.find(CardHolderUser.class, cardHolderUserId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public CardHolderUser getCardHolderUserByLogonName(String logonName) {
		String sql = "select * from CardHolderUser where logonname= ?1";
		Query query = _em.createNativeQuery(sql, CardHolderUser.class)
				.setParameter(1, logonName);
		List<CardHolderUser> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public CardHolderUser getCardHolderUserQuestionByLogonName(String logonName) {
		CardHolderUser cardHolderUser = getCardHolderUserByLogonName(logonName);
		if (null != cardHolderUser) {
			CardHolderUser returnCardHolderUser = new CardHolderUser();
			returnCardHolderUser.setCardHolderUserId(cardHolderUser
					.getCardHolderUserId());
			returnCardHolderUser.setQuestion1(cardHolderUser.getQuestion1());
			returnCardHolderUser.setQuestion2(cardHolderUser.getQuestion2());
			returnCardHolderUser.setQuestion3(cardHolderUser.getQuestion3());
			return returnCardHolderUser;
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardHolderUser> getCardHolderUsers(Integer page) {
		String sql = "select * from CardHolderUser";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, CardHolderUser.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardHolderUser> getCardHolderUsersByCondition(String email,
			String name, Integer idTypeId, String idNo, String sex,
			String status, Integer page) {
		String sql = "select * from CardHolderUser where 1=1 ";
		if (!PCPSUtil.isEmpty(email))
			sql += " and email = '" + email + "'";
		if (!PCPSUtil.isEmpty(name))
			sql += " and name = like '%" + name + "%'";
		if (null != idTypeId)
			sql += " and idtypeid = " + idTypeId;
		if (!PCPSUtil.isEmpty(idNo))
			sql += " and idno = '" + idNo + "'";
		if (!PCPSUtil.isEmpty(sex))
			sql += " and sex = '" + sex + "'";
		if (!PCPSUtil.isEmpty(status))
			sql += " and status = '" + status + "'";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, CardHolderUser.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getCardHolderUsersCount() {
		String sql = "select nvl(count(*),0) from CardHolderUser";
		Query query = _em.createNativeQuery(sql);
		return Integer.valueOf(query.getSingleResult().toString());
	}

	@Override
	public Integer getCardHolderUsersCountByCondition(String email,
			String name, Integer idTypeId, String idNo, String sex,
			String status) {
		String sql = "select nvl(count(*),0) from CardHolderUser where 1=1 ";
		if (!PCPSUtil.isEmpty(email))
			sql += " and email = '" + email + "'";
		if (!PCPSUtil.isEmpty(name))
			sql += " and name = like '%" + name + "%'";
		if (null != idTypeId)
			sql += " and idtypeid = " + idTypeId;
		if (!PCPSUtil.isEmpty(idNo))
			sql += " and idno = '" + idNo + "'";
		if (!PCPSUtil.isEmpty(sex))
			sql += " and sex = '" + sex + "'";
		if (!PCPSUtil.isEmpty(status))
			sql += " and status = '" + status + "'";
		Query query = _em.createNativeQuery(sql);
		return Integer.valueOf(query.getSingleResult().toString());
	}

	@Override
	public BigInteger getMaxCardHolderUserId() {
		Query query = _em
				.createNativeQuery("select nvl(max(cardHolderUserId),0)+1 cardHolderUserId from CardHolderUser");
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertCardHolderUser(CardHolderUser cardHolderUser)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, cardHolderUser);
	}

	@Override
	public void updateCardHolderUser(CardHolderUser cardHolderUser)
			throws PCPSApplicationException {
		GeneralDao.update(_em, cardHolderUser);
	}

	@Override
	public boolean checkEmailAlreadyRegister(String email) {
		String sql = "select nvl(count(*),0) from CardHolderUser where email=?1";
		Query query = _em.createNativeQuery(sql).setParameter(1, email);
		if (Integer.valueOf(query.getSingleResult().toString()) > 0) {
			return true;
		} else {
			return false;
		}
	}

}
